<?php
App::uses('AppModel', 'Model');
/**
 * Edition Model
 *
 * @property Livre $Livre
 * @property Editeur $Editeur
 * @property Exemplaire $Exemplaire
 */
class Edition extends AppModel {

/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'id';
	
	public $actsAs = array('Containable');
	
	// public $virtualFields = array(
		// 'date' => 'CONCAT(Edition.jour, "-", Edition.mois, "-", Edition.annee)'
	// );	

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'livre_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'editeur_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Livre' => array(
			'className' => 'Livre',
			'foreignKey' => 'livre_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Editeur' => array(
			'className' => 'Editeur',
			'foreignKey' => 'editeur_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasOne associations
 *
 * @var array
 */
	public $hasOne = array(
		'ExemplaireUtilisateur' => array(
			'className' => 'Exemplaire',
			'foreignKey' => 'edition_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Exemplaire' => array(
			'className' => 'Exemplaire',
			'foreignKey' => 'edition_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	
	public function afterFind($results, $primary = false) {
		foreach ($results as $key => $val) {
			$date="";
			if (isset($val['Edition']['jour'])) {
				$date.= sprintf("%02s", $val['Edition']['jour']).'-';
			}
			if (isset($val['Edition']['mois'])) {
				$date.=sprintf("%02s", $val['Edition']['mois']).'-';
			}
			if (isset($val['Edition']['annee'])) {
				$date.=$val['Edition']['annee'];
			}
			if (isset($val['Edition']['date'])) {
				$results[$key]['Edition']['date'] = $date ;
			}
			if (isset($val['Edition']['isbn10'])) {
				$results[$key]['Edition']['isbn10_format'] = $this->isbn10FormatAfterFind($val['Edition']['isbn10']) ;
			}
			
			if (isset($val['Edition']['isbn13'])) {
				$results[$key]['Edition']['isbn13_format'] = $this->isbn13FormatAfterFind($val['Edition']['isbn13']) ;
			}
			
			if (isset($val['Edition']['image'])) {
				if(!$val['Edition']['image'])
					$results[$key]['Edition']['image'] = '0.jpg' ;
			}
			
			if (isset($val['Edition']['ExemplaireUtilisateur']['possede'])) {
				$results[$key]['Edition']['ExemplaireUtilisateur']['est_possede'] = $this->estPossede($val['Edition']['ExemplaireUtilisateur']['possede']) ;
			}				
			
			if (isset($val['Edition']['ExemplaireUtilisateur']['lu'])) {
				$results[$key]['Edition']['ExemplaireUtilisateur']['est_lu'] = $this->estPossede($val['Edition']['ExemplaireUtilisateur']['lu']) ;
			}			
		}
		return $results;
	}

	public function dateFormatAfterFind($dateString) {
		return date('d-m-Y', strtotime($dateString));
	}
	public function isbn10FormatAfterFind($isbn10) {
		return substr($isbn10,0,1)."-".substr($isbn10,1,3)."-".substr($isbn10,4,5)."-".substr($isbn10,9,1);
	}
	public function isbn13FormatAfterFind($isbn13) {
		return substr($isbn13,0,3)."-".substr($isbn13,3,1)."-".substr($isbn13,4,3)."-".substr($isbn13,7,5)."-".substr($isbn13,12,1);
	}
	public function estPossede($possede) {
		return $possede=="1";
	}
	public function estLu($lu) {
		return $lu=="1";
	}
}
